package com.apps.realtyportal;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;

public class PropertyDetailsActivity extends SherlockActivity {

	String[] entries = new String[] { "Property Portal", "View Card",
			"Favorites", "Settings" };

	private EditText pname;
	private EditText type;
	private EditText price;
	private EditText description;
	private EditText oFname;
	private EditText oLname;
	private EditText address;
	private EditText agent;
	private TextView result;
	private Button ok;

	AlertDialog.Builder alertbox;

	private static String SOAP_ACTION1 = "http://createaccount.com/addProperty";
	private static String NAMESPACE = "http://createaccount.com/";
	private static String METHOD_NAME1 = "addProperty";
	private static String URL = "http://10.0.2.2:1000/CreateAccountWs/CreateAccount?wsdl";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.property_detail);

		ActionBar actionBar = getSupportActionBar();

		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);

		pname = (EditText) this.findViewById(R.id.pname);
		type = (EditText) this.findViewById(R.id.type);
		price = (EditText) this.findViewById(R.id.price);
		description = (EditText) this.findViewById(R.id.desc);
		oFname = (EditText) this.findViewById(R.id.oFname);
		oLname = (EditText) this.findViewById(R.id.oLname);
		address = (EditText) this.findViewById(R.id.address);
		agent = (EditText) this.findViewById(R.id.agent);
		ok = (Button) this.findViewById(R.id.btnRegister);

		alertbox = new AlertDialog.Builder(this);

	}

	public void Create(View v) {
		CreateAccount();

	}

	public void CreateAccount() {

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);

		request.addProperty("pname", pname.getText().toString());
		request.addProperty("type", type.getText().toString());
		request.addProperty("price",
				Integer.parseInt(price.getText().toString()));
		request.addProperty("status", "Available");
		request.addProperty("description", description.getText().toString());
		request.addProperty("lat", 10);
		request.addProperty("lon", 127);
		request.addProperty("address", address.getText().toString());
		request.addProperty("ownerF", oFname.getText().toString());
		request.addProperty("ownerL", oLname.getText().toString());
		request.addProperty("agent", agent.getText().toString());

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);

		try {

			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			// this is the actual part that will call the webservice
			androidHttpTransport.call(SOAP_ACTION1, envelope);
			// Get the SoapResult from the envelope body.
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

			if (response != null)

			{
				if (response.toString().equals("true")) {
					alertbox.setMessage("Success!");
					Toast.makeText(getApplicationContext(), "<<<<<<<<<<<<sud",
							Toast.LENGTH_LONG).show();
				} else {
					alertbox.setMessage("Unsuccessful");
				}

			} else {
				Toast.makeText(getApplicationContext(), "No Response",
						Toast.LENGTH_LONG).show();
			}
			alertbox.show();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * This callback method will be invoked when the user selects up navigation
	 * icon from the action bar
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, HomeActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
			startActivity(intent);
		}
		return super.onOptionsItemSelected(item);
	}

}
